package com.tuling.code.jvm.runtimearea;

/**
 * Jvm运行时数据区 之 虚拟机栈 虚拟机栈溢出
 * 
 * @author javaw
 *
 */
public class VmStackDemo {

	public static void main(String[] args) {
		//配置-Xss   -Xss128K
		/**
		VmStackOverFlowError sof = new VmStackOverFlowError();
		try {
			sof.stackLeak();
		} catch (Exception e) {
			System.out.println("stack length:" + sof.stackLength);
			e.printStackTrace();
		}
		*/
		
		JavaVMStackOOM oom = new JavaVMStackOOM();
		oom.stackLeakByThread();
	}
}

/**
 * 演示vm虚拟机栈 StackOverflowError
 * 
 * @author javaw
 *
 */
class VmStackOverFlowError {
	public int stackLength = 1;

	/**
	 * 反复调用
	 */
	public void stackLeak() {
		stackLength++;
		stackLeak();
	}
}

/**
 * 模拟VMStack OutOfMemoty
 * 
 * @author javaw
 *
 */
class JavaVMStackOOM {
	/**
	 * 死循环
	 */
	private void dontStop() {
		while (true) {
		}
	}

	public void stackLeakByThread() {
		while (true) {
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					dontStop();
				}
			});
			thread.start();
		}
	}
}